/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package amd.core.extraction;

import amd.core.CurrentDateAndTime;
import amd.core.data.DocumentPdf;
import amd.core.calculation.LsaCalculation;
import amd.core.calculation.TfIdfCalculation;
import amd.database.ConnectDB;
import amd.database.DbDocument;
import amd.database.DbTermDocument;
import java.io.File;
import java.io.FileFilter;
import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.util.PDFTextStripper;

/**
 *
 * @author Latifah Dwi Nurachma
 */
public class DocExtractor {

    public static void extractAllDocument(ConnectDB conn, File sourcePath, File targetPath) {
        
        System.out.println(CurrentDateAndTime.getDateTime() + " Ekstraksi dokumen");
        long startTime = System.currentTimeMillis()/1000;
        if(!sourcePath.isDirectory()){
            sourcePath = sourcePath.getParentFile();
        }

        //lakukan pembacaan seluruh file pada direktori
        File[] listOfFiles = sourcePath.listFiles(new FileFilter() {
            public boolean accept(File pathname) {
                return (pathname.getName().endsWith(".pdf"));
            }
        });

        int i =0;
        int nFiles = listOfFiles.length;
        if (nFiles>0){
            while (i<nFiles){
                try {
                    PDDocument doc = PDDocument.load(listOfFiles[i]);
                    DocumentPdf docPdf = extract(conn, listOfFiles[i]);
                    if (docPdf!=null){
                        doc.save(targetPath.getPath().concat("/".concat(saveDocPdfToDB(conn, docPdf)).concat(".pdf")));
                    }
                    doc.close();
                    listOfFiles[i].delete();

                } catch (Exception ex) {
                    //System.out.println("ada error saat mengekstak dokumen " + ex.toString());
                }
                i++;
            }

            //menghitung nilai bobot tf-idf lalu menyimpankannya pada database
            TfIdfCalculation.calculate(conn);

            //menghitung nilai bobot similarity antar dokumen dan menyimpankannya pada database
            LsaCalculation.calculate(conn);

            long endTime = System.currentTimeMillis()/1000;
            System.out.println("- Total waktu ekstraksi: " +
                    ((endTime-startTime)/3600) + " jam " +
                    ((endTime-startTime)/60) + " menit " +
                    ((endTime-startTime)%60) + " detik");
        }
        else{
            System.out.println("Tidak ada dokumen baru yang akan diekstrak.");
        }
    }

    public static DocumentPdf extract(ConnectDB conn, File f) {
        DocumentPdf docPdf = null;
        try {
            PDDocument doc = PDDocument.load(f);
            PDFTextStripper stripper = new PDFTextStripper();
            String pdfContent= stripper.getText(doc);
            String abstractDocument = getAbstract(pdfContent);
            if(abstractDocument.trim().isEmpty()){
                //printToLogFile("Dokumen \"" + f.getName()+"\" gagal diekstrak, tidak memiliki abstrak.");
                System.out.println("- Dokumen \"" + f.getName()+"\" gagal diekstrak, tidak memiliki abstrak.");
            }
            else{
                if(DbDocument.isExist(conn, JudulExtractor.extract(pdfContent))){
                    System.out.println("- Dokumen \"" + f.getName()+"\" gagal diekstrak, dokumen sudah ada sebelumnya.");
                }
                else if(DbDocument.isExist(conn, abstractDocument)){
                    //printToLogFile("Dokumen \"" + f.getName()+"\" gagal diekstrak, dokumen sudah ada sebelumnya.");
                    System.out.println("- Dokumen \"" + f.getName()+"\" gagal diekstrak, dokumen sudah ada sebelumnya.");
                }
                else{
                    docPdf = new DocumentPdf(f);
                    docPdf.setTitle(f.getName().substring(0, f.getName().length()-4));
                    docPdf.setLocation(f.getPath().replace('\\', '/'));
                    docPdf.setDocAbstract(abstractDocument);
                    docPdf.setTerm(TermsExtractor.extract(pdfContent));

                    System.out.println("- Dokumen \"" + f.getName()+"\" berhasil diekstrak.");

                    //printToLogFile("Dokumen \"" + f.getName()+"\" berhasil diekstrak.");
                }
            }
            doc.close();
        }
        catch (Exception e) {
            //System.out.println("error DocExtractor line 111 " + e.toString());
        }

        return docPdf;
    }

    private static String generateIdDoc(int nDoc) {
        String idDoc = ""+(nDoc+1);
        while(idDoc.length()<9){
            idDoc = "0".concat(idDoc);
        }
        return "doc"+idDoc;
    }

    private static String getAbstract(String stringInput) {
        String abstractDocument = "";
        abstractDocument = AbstractExtractor.extract(stringInput);
        return abstractDocument;
    }

    private static String saveDocPdfToDB(ConnectDB conn, DocumentPdf docPdf) {
        String idDoc = "";
        try {
            int nDoc = DbDocument.getNDocument(conn);
//            idDoc = generateIdDoc(nDoc)/*countAll(db_document)*/;
            //DbDocument.insert(conn, idDoc);
            DbDocument.insert(conn, String.valueOf(nDoc+1), docPdf.getTitle(), docPdf.getDocAbstract(), docPdf.getLocation(), docPdf.getPanjang());
            int i =0;
            while (i<docPdf.getTerm().size()){
                String termContent = docPdf.getTerm().get(i).getContent();
                int frequency = docPdf.getTerm().get(i).getFrequency();
                DbTermDocument.insert(conn, nDoc+1, termContent, frequency);
                i++;
            }
        } 
        catch(Exception e){}
        return idDoc;
    }


}
